import axios from 'axios';
import qs from 'qs';
import { message} from 'antd';
// import cookie from 'react-cookies';

let baseUrl = 'http://yk.huanghuai.edu.cn:7089';
axios.defaults.withCredentials = true;
axios.defaults.timeout = 3000000;
let token = sessionStorage.getItem("yiQingToken")

const instance = axios.create({
  xsrfCookieName: 'xsrf-token',
  baseURL: baseUrl,
  timeout: 3000000,
  responseType: 'json',
});

//添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    return config;
  },
  function (error) {
    //请求错误时做些事
    message.destroy();
    return Promise.reject(error);
  }
);

//添加一个响应拦截器
instance.interceptors.response.use(
  function (response) {
    if ((response.data.code === 20004&&token)||(response.data.code === 20009&&token)) {
      // Modal.error({
      //   title: '提示',
      //   content: response.data.message,
      // });
      sessionStorage.removeItem('yiQingToken')
      setTimeout(()=>{
        window.location.reload();
      },2000)
    }
    // 3.其他失败，比如校验不通过等
    return Promise.resolve(response);
  },
  res => {
    message.destroy();
    // 4.系统错误，比如500、404等
    // message.error('系统异常，请联系管理员！', 1);
    return Promise.reject({
      messageCode: 'sysError'
    });
  }
);

export default {
  get(url, param) {
    return new Promise(resolve => {
      instance({
        method: 'get',
        url,
        params: param,
        headers: { 'x-auth-token': token },
      }).then(res => {
        resolve(res.data);
      });
    }).catch((err) => { console.error(err) });
  },

  post(url, param) {
    return new Promise(resolve => {
      instance({
        method: 'post',
        url,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'x-auth-token': token
        },
        data: qs.stringify(param),
      }).then(res => {
        resolve(res.data);
      }).catch((err) => { console.error(err) });
    });
  },
  jsonPost(url, param) {
    return new Promise(resolve => {
      instance({
        method: 'post',
        url,
        headers: {
          'Content-Type': 'application/json',
          'x-auth-token': token
        },
        data: param,
      }).then(res => {
        resolve(res.data);
      }).catch((err) => { console.error(err) });
    });
  },
  upload(url, param) {
    return new Promise((resolve) => {
      instance({
        method: 'post',
        url,
        headers: { 'Content-Type': 'multipart/form-data', 'x-auth-token': token },
        data: param
      }).then(res => {
        resolve(res)
      }).catch(err => {
        console.error(err)
      })
    })
  },
  export(url, param, fileName) {
    return new Promise(resolve => {
      instance({
        method: 'get',
        url,
        params: param,
        responseType: 'blob',
      })
        .then(res => {
          const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
          const linkNode = document.createElement('a');
          linkNode.download = fileName; // a标签的download属性规定下载文件的名称
          linkNode.style.display = 'none';
          linkNode.href = URL.createObjectURL(blob); // 生成一个Blob URL
          document.body.appendChild(linkNode);
          linkNode.click(); // 模拟在按钮上的一次鼠标单击
          URL.revokeObjectURL(linkNode.href); // 释放URL 对象
          document.body.removeChild(linkNode);
          // resolve('导出成功');
        })
        .catch(err => {
          console.error(err);
        });
    });
  },
};
